package cs340.stormtroopers.hypeerweb;

import java.util.ArrayList;
import java.util.HashMap;

public class HyPeerWebBuilder
{
	private HashMap<Integer, Node> built;
	private boolean ready;

	/**
	 * The standard HyPeerWebBuilder constructor that builds a HashMap of Nodes
	 * from the given ArrayList of CompleteNodeDomains
	 * 
	 * @param nodes
	 *            A list of CompleteNodeDomains that need to be built into Node
	 *            objects.
	 * @pre nodes &ne; null
	 * @post this.built is a HashMap full of Node objects (mapped to Integers)
	 *       that have the correct WebId and Connections as given in their
	 *       respective CompleteNodeDomains
	 */
	public HyPeerWebBuilder(ArrayList<CompleteNodeDomain> nodes)
	{
		built = new HashMap<Integer, Node>();
		ready = false;

		for (CompleteNodeDomain singleDomain : nodes)
		{
			Node singleNode = new Node(singleDomain.getWebId(),
					singleDomain.getHeight());
			built.put(singleNode.getWebIdValue(), singleNode);
		}

		for (CompleteNodeDomain singleDomain : nodes)
		{
			Node current = built.get(singleDomain.getWebId());
			Connections connection = new Connections();

			if (singleDomain.getFold() == -1)
				connection.setFold(Node.NULL_NODE);
			else
				connection.setFold(built.get(singleDomain.getFold()));

			if (singleDomain.getInverseSurrogateFold() == -1)
				connection.setInverseSurrogateFold(Node.NULL_NODE);
			else
				connection.setInverseSurrogateFold(built.get(singleDomain
						.getInverseSurrogateFold()));

			if (singleDomain.getSurrogateFold() == -1)
				connection.setSurrogateFold(Node.NULL_NODE);
			else
				connection.setSurrogateFold(built.get(singleDomain
						.getSurrogateFold()));

			ArrayList<Node> downPointers = new ArrayList<Node>();
			for (Integer i : singleDomain.getDownPointers())
				downPointers.add(built.get(i));
			connection.setDownPointers(downPointers);

			ArrayList<Node> upPointers = new ArrayList<Node>();
			for (Integer i : singleDomain.getUpPointers())
				upPointers.add(built.get(i));
			connection.setUpPointers(upPointers);

			ArrayList<Node> neighborsBiggerThanMe = new ArrayList<Node>();
			for (Integer i : singleDomain.getNeighborsBiggerThanMe())
				neighborsBiggerThanMe.add(built.get(i));
			connection.setNeighborsBiggerThanMe(neighborsBiggerThanMe);

			ArrayList<Node> childlessNeighborsSmallerThanMe = new ArrayList<Node>();
			for (Integer i : singleDomain.getChildlessNeighborsSmallerThanMe())
				childlessNeighborsSmallerThanMe.add(built.get(i));
			connection
					.setChildlessNeighborsSmallerThanMe(childlessNeighborsSmallerThanMe);

			ArrayList<Node> parentNeighborsSmallerThanMe = new ArrayList<Node>();
			for (Integer i : singleDomain.getParentNeighborsSmallerThanMe())
				parentNeighborsSmallerThanMe.add(built.get(i));
			connection
					.setParentNeighborsSmallerThanMe(parentNeighborsSmallerThanMe);

			current.setConnections(connection);
		}

		ready = true;
	}

	/**
	 * Returns the HashMap of Nodes created in the constructor
	 * 
	 * @pre HyPeerWeb constructor finished successfully
	 * @post none
	 */
	public HashMap<Integer, Node> getNodeSet()
	{
		if (!ready)
			return null;
		else
			return built;
	}
}
